package com.service.Impl;

import com.dto.LotteryRecordWithPrizeDto;
import com.entity.LotteryOrder;
import com.exception.UpdateException;
import com.github.pagehelper.PageInfo;
import com.mapper.LotteryOrderMapper;
import com.mapper.LotteryPrizeMapper;
import com.mapper.UserMapper;
import com.service.LotteryOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName LotteryOrderServiceImpl
 * @Description 奖品订单业务层实现类
 * @Author Kx
 * @Date 2025/6/19 15:53
 * @Version 1.0
 */
@Service("lotteryOrderService")
public class LotteryOrderServiceImpl implements LotteryOrderService {
    private final LotteryOrderMapper lotteryOrderMapper;
    private final UserMapper userMapper;
    private final LotteryPrizeMapper lotteryPrizeMapper;

    public LotteryOrderServiceImpl(LotteryOrderMapper lotteryOrderMapper, UserMapper userMapper, LotteryPrizeMapper lotteryPrizeMapper) {
        this.lotteryOrderMapper = lotteryOrderMapper;
        this.userMapper = userMapper;
        this.lotteryPrizeMapper = lotteryPrizeMapper;
    }

    /**
     * @author: kx
     * @date: 2025/6/19 15:57
     * @param lotteryOrder : 奖品订单信息
     * @description: 修改奖品订单状态
    */
    @Transactional
    @Override
    public void updateLotteryOrderStatus(LotteryOrder lotteryOrder) {
        try {
            // 设置更新时间
            lotteryOrder.setUpdateTime(LocalDateTime.now());
            lotteryOrderMapper.updateLotteryOrderStatus(lotteryOrder);
        } catch (Exception e) {
            throw new UpdateException(1003, "修改奖品订单状态失败");
        }
    }

    /**
     * @author: kx
     * @date: 2025/6/19 16:13
     * @param pageNum : 页码
     * @param pageSize : 每页显示数量
     * @return com.github.pagehelper.PageInfo<com.dto.LotteryRecordWithPrizeDto>
     * @description: 分页查询所有奖品订单
    */
    @Override
    public PageInfo<LotteryRecordWithPrizeDto> getPagedLotteryOrders(Integer pageNum, Integer pageSize) {
        if (!lotteryOrderMapper.getPagedLotteryOrders(pageNum, pageSize).isEmpty()) {
            // 获取所有奖品订单信息
            List<LotteryOrder> lotteryOrders = lotteryOrderMapper.getPagedLotteryOrders(pageNum, pageSize);
            // 创建传输对象集合
            List<LotteryRecordWithPrizeDto> lotteryRecordWithPrizeDtoList = new ArrayList<>();
            for (LotteryOrder lotteryOrder : lotteryOrders) {
                // 创建传输对象
                LotteryRecordWithPrizeDto lotteryRecordWithPrizeDto = new LotteryRecordWithPrizeDto();
                // 设置用户信息
                lotteryRecordWithPrizeDto.setUser(userMapper.findByUserId(lotteryOrder.getUserId()));
                // 设置奖品信息
                lotteryRecordWithPrizeDto.setLotteryPrize(lotteryPrizeMapper.getLotteryPrizeById(lotteryOrder.getPrizeId()));
                // 添加到集合中
                lotteryRecordWithPrizeDtoList.add(lotteryRecordWithPrizeDto);
            }
            return new PageInfo<>(lotteryRecordWithPrizeDtoList);
        }
        throw new UpdateException(1001, "没有查询到奖品订单信息");
    }
}
